#include "PlayerArea.h"
#include "Character\MagicObject.h"
#include "Character\PlayerCharacter.h"
#include "ISpell.h"
#include "qimage.h"
#include <qwidget.h>
#include <qpainter.h>

PlayerArea::PlayerArea(PlayerCharacter * iPlayer, QWidget * parent) : 
QWidget(parent)
	, mPlayer(iPlayer)
	, mBackground(0)
	, greenCone(0)
	, redCone(0)
	, blueCone(0)
	, goldCoin(0)
	, fateToken(0)
{
	mBackground = new QImage();
	mBackground->load("Images\\area3.gif");

	greenCone= new QImage();
	redCone= new QImage();
	blueCone= new QImage();
	goldCoin = new QImage();
	fateToken = new QImage();
	greenCone->load("Images\\greenCone.gif");
	redCone->load("Images\\redCone.gif");
	blueCone->load("Images\\blueCone.gif"); 
	goldCoin->load("Images\\goldCoin.gif"); 
	fateToken->load("Images\\fateToken.gif"); 
	setFixedSize(400, 350);
	setContentsMargins(0, 0, 0, 0);
}

PlayerArea::~PlayerArea()
{
	if (0 != mBackground)
		delete mBackground;
	if (0 != greenCone)
		delete greenCone;
	if (0 != redCone)
		delete redCone;
	if (0 != blueCone)
		delete blueCone;
	if (0 != goldCoin)
		delete goldCoin;
	if (0 != fateToken)
		delete fateToken;
}

void PlayerArea::paintEvent(QPaintEvent * event)
{
	QPainter qpainter(this);
    qpainter.drawImage(0, 0, *mBackground);
	qpainter.drawImage(85, 30, *(mPlayer->getPicture()));
	qpainter.drawText(164, 22, QString(mPlayer->getName().c_str()));
	QRect r (212, 34, 115, 165);
	QFont f = qpainter.font();
	f.setPointSize(7);
	qpainter.setFont(f);
	qpainter.drawText(r, mPlayer->getDescription().c_str());

	for (int i = 0; i < mPlayer->getStrengthCounter() ; ++i)
	{
		int x = i * 4;
		int y = i * 4;
		if (i % 2 == 0)
			x += 15;

		qpainter.drawImage(x,y,redCone->scaledToHeight(30));
	}

	for (int i = 0; i < mPlayer->getCraftCounter() ; ++i)
	{
		int x = i * 4;
		int y = 100 + i * 4;
		if (i % 2 == 0)
			x += 15;

		qpainter.drawImage(x,y,blueCone->scaledToHeight(30));
	}
	
	for (int i = 0; i < mPlayer->getFateTokenNumberInPossession() ; ++i)
	{
		int x = 330 + i * 4;
		int y = i * 4;
		if (i % 2 == 0)
			x += 15;

		qpainter.drawImage(x,y,fateToken->scaledToHeight(30));
	}

	for (int i = 0; i < mPlayer->getSpells().size(); ++i)
	{
		int x = 0;
		int y = 200 + i * 52;
		qpainter.drawImage(x, y, mPlayer->getSpells()[i]->getImage());
	}

	for (int i = 0; i < mPlayer->getGoldenCoinNumberInPossession(); ++i)
	{
		int x = 330 + i * 4;
		int y =80  + i * 8;
		if (i % 2 == 0)
			x += 15;

		qpainter.drawImage(x,y,goldCoin->scaledToHeight(30));
	}

	for (int i = 0; i < mPlayer->getLifeCounter() ; ++i)
	{
		int x = 330 + i * 4;
		int y = 180 + i * 4;
		if (i % 2 == 0)
			x += 15;

		qpainter.drawImage(x,y,greenCone->scaledToHeight(30));
	}

	int i = 0;
	for (; i < mPlayer->getMagicObjects().size(); ++i)
		qpainter.drawImage(i * 90 + 60, 250, mPlayer->getMagicObjects()[i]->getPicture()->scaledToWidth(80));

	int j = i;

	for (i = 0; i < mPlayer->getFollowers().size(); ++i)
		qpainter.drawImage(j * 90 + i * 130  +60 , 250, mPlayer->getFollowers()[i]->getPicture()->scaledToWidth(120));

		
}
